package cn.com.thingo.shyintelligent.ui.activity;


import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.ForgetPasswordContract;
import cn.com.thingo.shyintelligent.model.bean.CommonBean;
import cn.com.thingo.shyintelligent.presenter.ForgetPasswordPresenter;
import cn.com.thingo.shyintelligent.util.CountDownTimerUtils;
import cn.com.thingo.shyintelligent.util.LogUtil;
import cn.com.thingo.shyintelligent.util.NumberVerifiedUtils;
import cn.com.thingo.shyintelligent.util.ToastUtil;
import cn.com.thingo.shyintelligent.widget.ClearEditText;

public class ForgetPasswordActivity extends BaseActivity<ForgetPasswordPresenter> implements ForgetPasswordContract.View {
    @BindView(R.id.forget_phone_et)
    ClearEditText etPhone;
    @BindView(R.id.forget_code_et)
    EditText etCode;
    @BindView(R.id.forget_get_code)
    TextView tvGetCode;
    @BindView(R.id.forget_set_password_et)
    EditText etPassword;
    @BindView(R.id.forget_confirm_password_et)
    EditText etPasswordAgain;

    @Override
    protected int getLayout() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initEventAndData() {

    }

    @OnClick({R.id.forget_submit, R.id.forget_get_code})
    public void onClick(View view) {
        String telephone = etPhone.getText().toString().trim();
        switch (view.getId()) {
            case R.id.forget_submit:
                //修改提交
                String password = etPassword.getText().toString().trim();
                String passwordAgain = etPasswordAgain.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(telephone)) {
                    ToastUtil.shortShow("手机号码不能为空！");
                } else if (TextUtils.isEmpty(password)) {
                    ToastUtil.shortShow("密码不能为空！");
                } else if (!TextUtils.equals(password, passwordAgain)) {
                    ToastUtil.shortShow("两次密码不一致！");
                } else if (TextUtils.isEmpty(code)) {
                    ToastUtil.shortShow("验证码不能为空！");
                } else {
                    mPresenter.modifyPassword(password, telephone, code);
                }
                break;
            case R.id.forget_get_code:
                //获取验证码
                if (TextUtils.isEmpty(telephone)) {
                    ToastUtil.shortShow("手机号码不能为空！");
                } else if (NumberVerifiedUtils.isTelephone(telephone)) {
                    CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvGetCode, 60000, 1000);
                    countDownTimerUtils.start();
                    mPresenter.sendCode(telephone);
                } else {
                    ToastUtil.shortShow("请输入正确的电话号码！");
                }
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void viewGetCode(CommonBean commonBean) {
        String msg = commonBean.getMsg();
        ToastUtil.shortShow(msg);
        LogUtil.d(msg);
    }

    @Override
    public void viewModifyPassword(CommonBean commonBean) {
        ToastUtil.shortShow("密码修改成功！");
        finish();
    }
}
